home *** CD-ROM | disk | FTP | other *** search
/ Aminet 19 / Aminet 19 (1997)(GTI - Schatztruhe)[!][Jun 1997].iso / Aminet / dev / c / vbcc.lha / vbcc / declaration.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-04  |  54.6 KB  |  1,440 lines

  1. /*  $VER: vbcc (declaration.c) V0.4     */
  2.  
  3. #include "vbc.h"
  4.  
  5. static char FILE_[]=__FILE__;
  6.  
  7. #define PARAMETER 8
  8. #define OLDSTYLE 16
  9.  
  10. struct const_list *initialization(struct Typ *,int);
  11. int test_assignment(struct Typ *,np);
  12. int return_sc,return_reg,has_return;
  13.  
  14. extern int float_used;
  15. extern void optimize(long,struct Var *);
  16.  
  17. int settyp(int typnew, int typold)
  18. /* Unterroutine fuer declaration_specifiers()               */
  19. {
  20.     static int warned_long_double;
  21.     if(DEBUG&2) printf("settyp: new=%d old=%d\n",typnew,typold);
  22.     if(typold==LONG&&typnew==FLOAT){ error(203); return(DOUBLE);}
  23.     if(typold==LONG&&typnew==DOUBLE){
  24.         if(!warned_long_double){error(204);warned_long_double=1;}
  25.         return(DOUBLE);
  26.     }
  27.     if(typold!=0&&typnew!=INT){error(47);return(typnew);}
  28.     if(typold==0&&typnew==INT) return(INT);
  29.     if(typold==0) return(typnew);
  30.     if(typold==SHORT||typold==LONG) return(typold);
  31.     error(48);
  32.     return(typnew);
  33. }
  34.  
  35. #define dsc if(storage_class) error(49); if(typ||type_qualifiers) error(50)
  36. #define XSIGNED 16384
  37.  
  38. struct Typ *declaration_specifiers(void)
  39. /* Erzeugt neuen Typ und gibt Zeiger darauf zurueck,      */
  40. /* parst z.B. unsigned int, struct bla etc.               */
  41. /* evtl. muessen noch storage-classes uns strengere       */
  42. /* Pruefungen etc. eingebaut werden                       */
  43. {
  44.     int typ=0,type_qualifiers=0,notdone,storage_class,hard_reg;
  45.     char *merk,*imerk,sident[MAXI],sbuff[MAXI];
  46.     struct Typ *new=mymalloc(TYPS),*t,*ts;
  47.     struct struct_declaration *ssd;
  48.     struct struct_list (*sl)[];
  49.     size_t slsz;
  50.     struct Var *v;
  51.     storage_class=hard_reg=0;
  52.     new->next=0; new->exact=0;
  53.     do{
  54.         merk=s;killsp();cpbez(buff,0);notdone=0;
  55.         if(DEBUG&2) printf("ts: %s\n",buff);
  56.         if(!strcmp("struct",buff)) notdone=STRUCT;
  57.         if(!strcmp("union",buff)) notdone=UNION;
  58.         if(notdone!=0){
  59.             killsp();
  60.             if(*s!='{'){
  61.                 cpbez(sident,1);
  62.                 killsp();
  63.                 ssd=find_struct(sident,0);
  64.                 if(ssd&&*s=='{'&&find_struct(sident,nesting)&&ssd->count>0) error(13,sident);
  65.                 if(!ssd||((*s=='{'||*s==';')&&!find_struct(sident,nesting))){
  66.                     typ=settyp(notdone,typ);
  67.                     ssd=mymalloc(sizeof(*ssd));
  68.                     ssd->count=0;
  69.                     new->exact=ssd=add_sd(ssd);
  70.                     add_struct_identifier(sident,ssd);
  71.                 }else{
  72.                     new->exact=ssd;
  73.                     typ=settyp(new->flags=notdone,typ);
  74.                 }
  75.             }else{
  76.                 *sident=0;
  77.                 typ=settyp(notdone,typ);
  78.                 ssd=mymalloc(sizeof(*ssd));
  79.                 ssd->count=0;
  80.                 new->exact=ssd=add_sd(ssd);
  81.             }
  82.             if(*s=='{'){
  83.                 s++;
  84.                 killsp();
  85.                 slsz=SLSIZE;
  86.                 sl=mymalloc(slsz*sizeof(struct struct_list));
  87.                 ssd->count=0;
  88.                 imerk=ident;
  89.                 ts=declaration_specifiers();
  90.                 while(*s!='}'&&ts){
  91.                     ident=sbuff;
  92.                     t=declarator(clone_typ(ts));
  93.                     killsp();
  94.                     if(*s==':'){
  95.                     /*  bitfields werden hier noch ignoriert    */
  96.                         np tree;
  97.                         if((ts->flags&15)!=INT) error(51);
  98.                         s++;killsp();tree=assignment_expression();
  99.                         if(type_expression(tree)){
  100.                             if(tree->flags!=CEXPR) error(52);
  101.                             if((tree->ntyp->flags&15)<CHAR||(tree->ntyp->flags&15)>LONG) error(52);
  102.                         }
  103.                         if(tree) free_expression(tree);
  104.                     }else{
  105.                         if(*ident==0) error(53);
  106.                     }
  107.                     if(type_uncomplete(t)){
  108.                         error(14,sbuff);
  109.                         freetyp(t);
  110.                         break;
  111.                     }
  112.                     if((t->flags&15)==FUNKT)
  113.                         error(15,sbuff);
  114.  
  115.                     if(*ident!=0){
  116.                         int i=ssd->count;
  117.                         while(--i>=0)
  118.                             if(!strcmp((*sl)[i].identifier,ident))
  119.                                 error(16,ident);
  120.                     }
  121.                     (*sl)[ssd->count].styp=t;
  122.                     (*sl)[ssd->count].identifier=add_identifier(ident,strlen(ident));
  123.                     ssd->count++;
  124.                     if(ssd->count>=slsz-1){
  125.                         slsz+=SLSIZE;
  126.                         sl=realloc(sl,slsz*sizeof(struct struct_list));
  127.                         if(!sl){error(12);raus();}
  128.                     }
  129.                     killsp();
  130.                     if(*s==',') {s++;killsp();continue;}
  131.                     if(*s!=';') error(54); else s++;
  132.                     killsp();
  133.                     if(*s!='}'){
  134.                         if(ts) freetyp(ts);
  135.                         ts=declaration_specifiers();killsp();
  136.                     }
  137.                 }
  138.                 if(ts) freetyp(ts);
  139.                 if(ssd->count==0) error(55);
  140.                 ident=imerk;
  141.                 add_sl(ssd,sl);
  142.                 free(sl);
  143.                 if(*s!='}') error(56); else s++;
  144.                 new->flags=notdone|type_qualifiers;
  145.             }
  146.             notdone=1;
  147.         }
  148.         if(!strcmp("enum",buff)){
  149.         /*  enumerations; die Namen werden leider noch ignoriert    */
  150.             killsp();notdone=1;
  151.             if(*s!='{'){cpbez(buff,1);killsp();}
  152.             if(*s=='{'){
  153.                 zlong val; struct Var *v; struct Typ *t;
  154.                 val=l2zl(0L);
  155.                 s++;killsp();
  156.                 while(*s!='}'){
  157.                     cpbez(sident,1);killsp();
  158.                     if(*sident==0) {error(56);break;}
  159.                     t=mymalloc(TYPS);
  160.                     t->flags=CONST|INT;
  161.                     t->next=0;
  162.                     if(find_var(sident,nesting)) error(17,sident);
  163.                     v=add_var(sident,t,AUTO,0); /*  AUTO hier klug? */
  164.                     if(*s=='='){
  165.                         s++;killsp();
  166.                         v->clist=initialization(v->vtyp,0);
  167.                         val=zi2zl(v->clist->val.vint);killsp();
  168.                     }else{
  169.                         v->clist=mymalloc(CLS);
  170.                         v->clist->val.vint=val;
  171.                         v->clist->next=v->clist->other=0;
  172.                         v->clist->tree=0;
  173.                     }
  174.                     vlong=l2zl(1L);val=zladd(val,vlong);
  175.                     v->vtyp->flags=CONST|ENUM;
  176.                     if(*s!='}'&&*s!=',') {error(56);break;}
  177.                     if(*s==',') s++;
  178.                     killsp();
  179.                     if(*s=='}') {s++; break;}
  180.                 }
  181.             }
  182.             killsp();
  183.             typ=settyp(INT,typ);*buff=0;
  184.         }
  185.         if(!strcmp("void",buff)) {typ=settyp(VOID,typ);notdone=1;}
  186.         if(!strcmp("char",buff)) {typ=settyp(CHAR,typ);notdone=1;}
  187.         if(!strcmp("short",buff)) {typ=settyp(SHORT,typ);notdone=1;}
  188.         if(!strcmp("int",buff)) {typ=settyp(INT,typ);notdone=1;}
  189.         if(!strcmp("long",buff)) {typ=settyp(LONG,typ);notdone=1;}
  190.         if(!strcmp("float",buff)) {typ=settyp(FLOAT,typ);notdone=1;}
  191.         if(!strcmp("double",buff)) {typ=settyp(DOUBLE,typ);notdone=1;}
  192.         if(!strcmp("const",buff)){
  193.             if(type_qualifiers&CONST) error(58);
  194.             type_qualifiers|=CONST;notdone=1;
  195.         }
  196.         if(!strcmp("volatile",buff)){
  197.             if(type_qualifiers&VOLATILE) error(58);
  198.             type_qualifiers|=VOLATILE;notdone=1;
  199.         }
  200.         if(!strcmp("unsigned",buff)){
  201.             if(type_qualifiers&(XSIGNED|UNSIGNED)) error(58);
  202.             notdone=1;type_qualifiers|=UNSIGNED;
  203.         }
  204.         if(!strcmp("signed",buff)){
  205.             if(type_qualifiers&(XSIGNED|UNSIGNED)) error(58);
  206.             notdone=1;type_qualifiers|=XSIGNED;
  207.         }
  208.         if(!strcmp("auto",buff)) {dsc;storage_class=AUTO;notdone=1;}
  209.         if(!strcmp("register",buff)){dsc;storage_class=REGISTER;notdone=1;}
  210.         if(!strcmp("static",buff)) {dsc;storage_class=STATIC;notdone=1;}
  211.         if(!strcmp("extern",buff)) {dsc;storage_class=EXTERN;notdone=1;}
  212.         if(!strcmp("typedef",buff)) {dsc;storage_class=TYPEDEF;notdone=1;}
  213.         if(!(c_flags[7]&USEDFLAG)&&!strcmp("__reg",buff)){
  214.             char *d;int f=0;
  215.             killsp(); if(*s=='(') s++; else error(151);
  216.             killsp(); if(*s=='\"') s++; else error(74);
  217.             d=buff;
  218.             while(*s&&*s!='\"'){
  219.                 if(d-buff-2>MAXI){
  220.                     if(!f){ error(206,MAXI);f=1;}
  221.                 }else *d++=*s;
  222.                 s++;
  223.             }
  224.             *d=0;
  225.             if(*s=='\"') s++; else error(74);
  226.             killsp(); if(*s==')') s++; else error(59);
  227.             for(hard_reg=1;hard_reg<=MAXR;hard_reg++){
  228.                 if(!strcmp(buff,regnames[hard_reg])) break;
  229.             }
  230.             if(hard_reg>MAXR){ hard_reg=0;error(220,buff);}
  231.             notdone=1;
  232.         }
  233.  
  234.         if(!notdone&&*buff&&typ==0&&!(type_qualifiers&(XSIGNED|UNSIGNED))){
  235.             v=find_var(buff,0);
  236.             if(v&&v->storage_class==TYPEDEF){
  237.                 free(new);
  238.                 new=clone_typ(v->vtyp);
  239.                 typ=settyp(new->flags,typ);
  240.                 notdone=1;
  241.             }
  242.         }
  243.         if(DEBUG&2) printf("typ:%d\n",typ);
  244.         killsp();
  245.     }while(notdone);
  246.     s=merk;
  247.     return_sc=storage_class;
  248.     return_reg=hard_reg;
  249.     if(typ==0){
  250.         if(storage_class==0&&type_qualifiers==0) {free(new);return(0);}
  251.         typ=INT;
  252.     }
  253.     if(type_qualifiers&(XSIGNED|UNSIGNED))
  254.         if(typ!=INT&&typ!=CHAR&&typ!=LONG&&typ!=SHORT)
  255.             error(58);
  256.     if(DEBUG&2) printf("ts finish:%s\n",s);
  257.     new->flags=typ|type_qualifiers;
  258.     return(new);
  259. }
  260.  
  261. struct Typ *declarator(struct Typ *a)
  262. /* Erzeugt einen neuen Typ, auf Basis des Typs a            */
  263. /* a wird hiermit verkettet                                 */
  264. {
  265.     struct Typ *t;
  266.     killsp();*ident=0;
  267.     t=direct_declarator(pointer(a));
  268.     if(!a) {if(t) freetyp(t);return(0);} else return(t);
  269. }
  270. struct Typ *pointer(struct Typ *a)
  271. /* Unterroutine fuer declaration(), behandelt Zeiger auf Typ    */
  272. /* Kann sein, dass die Assoziativitaet nicht stimmt */
  273. {
  274.     struct Typ *t;char *merk;int notdone;
  275.     if(!a) return(0);
  276.     killsp();
  277.     while(*s=='*'){
  278.         s++;
  279.         t=mymalloc(TYPS);
  280.         t->flags=POINTER;
  281.         t->next=a;
  282.         a=t;
  283.         do{
  284.             killsp();
  285.             merk=s;cpbez(buff,0);
  286.             notdone=0;
  287.             if(!strcmp("const",buff)) {a->flags|=CONST;notdone=1;}
  288.             if(!strcmp("volatile",buff)) {a->flags|=VOLATILE;notdone=1;}
  289.         }while(notdone);
  290.         s=merk;
  291.     }
  292.     return(a);
  293. }
  294.  
  295. struct Typ *direct_declarator(struct Typ *a)
  296. /*  Unterroutine zu declarator()                    */
  297. /* behandelt [],(funkt),(dekl)                      */
  298. /* Funktionesufrufe und Arrays noch unvollstaendig  */
  299. /* implementiert, auch Assoziativitaet zweifelhaft  */
  300. {
  301.     struct Typ *rek=0,*merk,*p,*t,*first,*last=0;
  302.     struct struct_declaration *fsd;
  303.     struct struct_list (*sl)[];
  304.     size_t slsz;
  305.     char *imerk,fbuff[MAXI];
  306.     killsp();
  307.     if(!isalpha((unsigned char)*s)&&*s!='_'&&*s!='('&&*s!='[') return(a);
  308.     if(isalpha((unsigned char)*s)||*s=='_'){
  309.         cpbez(ident,1);
  310.         if(!a) return(0);
  311.     }else if(*s=='('&&a){
  312.         /* Rekursion */
  313.         imerk=s; s++; killsp();
  314.         if(*s!=')'&&*ident==0&&!declaration(0)){
  315.             merk=a;
  316.             rek=declarator(a);
  317.             if(*s!=')') error(59); else s++;
  318.         }else s=imerk;
  319.     }
  320.     if(!a)return(0);
  321.     killsp();
  322.     while(*s=='['||*s=='('){
  323.         if(*s=='['){
  324.             s++;
  325.             killsp();
  326.             p=mymalloc(TYPS);
  327.             p->flags=ARRAY;
  328.             p->next=0;
  329.             if(*s==']'){
  330.                 p->size=l2zl(0L);
  331.             }else{
  332.                 np tree;
  333.                 tree=expression();
  334.                 if(!type_expression(tree)){
  335. /*                    error("incorrect constant expression");*/
  336.                 }else{
  337.                     if(tree->sidefx) error(60);
  338.                     if(tree->flags!=CEXPR||(tree->ntyp->flags&15)<CHAR||(tree->ntyp->flags&15)>LONG){
  339.                         error(19);
  340.                     }else{
  341.                         eval_constn(tree);
  342.                         p->size=vlong;
  343.                         if(zleqto(p->size,l2zl(0L))) {error(61);p->size=l2zl(1L);}
  344.                     }
  345.                 }
  346.                 free_expression(tree);
  347.             }
  348.             if(*s!=']') error(62); else s++;
  349.             if(last){
  350.                 last->next=p;
  351.                 last=p;
  352.             }else{
  353.                 first=last=p;
  354.             }
  355.         }
  356.         if(*s=='('){
  357.             int komma;
  358.             /* Identifier- oder Parameter-list noch nicht komplett */
  359.             /* z.B. ... oder ohne Parameter                        */
  360.             s++;
  361.             killsp();
  362.             fsd=mymalloc(sizeof(*fsd));
  363.             slsz=SLSIZE;
  364.             sl=mymalloc(sizeof(struct struct_list)*slsz);
  365.             fsd->count=0;
  366.             imerk=ident;komma=0;
  367.             enter_block();
  368.             while(*s!=')'&&*s!='.'){
  369.                 ident=fbuff;*fbuff=0;komma=0;
  370.                 t=declarator(declaration_specifiers());
  371.                 if(!t&&*ident==0) {error(20);
  372.                                    break;}
  373.                 if(fsd->count){
  374.                     if((t&&!(*sl)[fsd->count-1].styp)||
  375.                        (!t&&(*sl)[fsd->count-1].styp))
  376.                         error(63);
  377.                 }
  378.                 if(!return_sc) return_sc=AUTO;
  379.                 if(return_sc!=AUTO&&return_sc!=REGISTER)
  380.                     {error(21);return_sc=AUTO;}
  381.                 (*sl)[fsd->count].styp=t;
  382.                 (*sl)[fsd->count].storage_class=return_sc;
  383.                 (*sl)[fsd->count].reg=return_reg;
  384.                 if(return_reg&&!regok(return_reg,t->flags,0)) error(217,regnames[return_reg]);
  385.                 (*sl)[fsd->count].identifier=add_identifier(ident,strlen(ident));
  386.                 if(t){
  387.                     if(((*sl)[fsd->count].styp->flags&15)==VOID&&fsd->count!=0)
  388.                         error(22);
  389.                     /*  Arrays in Zeiger umwandeln  */
  390.                     if(((*sl)[fsd->count].styp->flags&15)==ARRAY)
  391.                         (*sl)[fsd->count].styp->flags=POINTER;
  392.                     /*  Funktionen in Zeiger auf Funktionen umwandeln   */
  393.                     if(((*sl)[fsd->count].styp->flags&15)==FUNKT){
  394.                         struct Typ *new;
  395.                         new=mymalloc(TYPS);
  396.                         new->flags=POINTER;
  397.                         new->next=(*sl)[fsd->count].styp;
  398.                         (*sl)[fsd->count].styp=new;
  399.                     }
  400.  
  401.                 }
  402.                 fsd->count++;
  403.                 if(fsd->count>=slsz-2){     /*  eins Reserve fuer VOID  */
  404.                     slsz+=SLSIZE;
  405.                     sl=realloc(sl,slsz*sizeof(struct struct_list));
  406.                     if(!sl){error(12);raus();}
  407.                 }
  408.                 killsp(); /* Hier Syntaxpruefung strenger machen */
  409.                 if(*s==',') {s++;komma=1; killsp();}
  410.             }
  411.             ident=imerk;
  412.             if((*s!='.'||*(s+1)!='.'||*(s+2)!='.')||!komma){
  413.                 if(fsd->count>0&&(!(*sl)[fsd->count-1].styp||((*sl)[fsd->count-1].styp->flags&15)!=VOID)){
  414.                     (*sl)[fsd->count].styp=mymalloc(TYPS);
  415.                     (*sl)[fsd->count].styp->flags=VOID;
  416.                     (*sl)[fsd->count].styp->next=0;
  417.                     (*sl)[fsd->count].identifier=empty;
  418.                     fsd->count++;
  419.                 }
  420.             }else if(komma) {s+=3;komma=0;}
  421.             p=mymalloc(TYPS);
  422.             p->flags=FUNKT;
  423.             p->next=0;
  424.             {
  425.                 int m=nesting;
  426.                 nesting=0;
  427.                 p->exact=add_sd(fsd);
  428.                 add_sl(fsd,sl);
  429.                 free(sl);
  430.                 nesting=m;
  431.             }
  432.             killsp();
  433.             if(komma) error(59);
  434.             if(*s!=')') error(59); else s++;
  435.             killsp();
  436.             if(*s==','||*s==';'||*s==')'||*s=='=') leave_block();
  437.             if(last){
  438.                 last->next=p;
  439.                 last=p;
  440.             }else{
  441.                 first=last=p;
  442.             }
  443.         }
  444.         killsp();
  445.     }
  446.     if(last){last->next=a;last=a;a=first;}
  447.     if(rek!=0&&rek!=merk){
  448.         /* Zweite Liste anhaengen */
  449.         p=rek;
  450.         while(p->next!=merk) p=p->next;
  451.         if(p) p->next=a; else ierror(0);
  452.         return(rek);
  453.     }
  454.     return(a);
  455. }
  456. int declaration(int offset)
  457. /*  Testet, ob eine Typangabe kommt. Wenn offset!=0 ist,    */
  458. /*  muss s auf '(' zeigen und es wird getestet, ob nach der */
  459. /*  Klammer eine Typangabe kommt.                           */
  460. /*  In jedem Fall zeigt s danach wieder auf dieselbe Stelle */
  461. /*  im Source.                                              */
  462. {
  463.     char *merk=s,buff[MAXI];
  464.     struct Var *v;
  465.     if(offset){
  466.         s++;
  467.         read_new_line=0;
  468.         if(DEBUG&1) printf("cleared read_new_line\n");
  469.         killsp();
  470.         if(read_new_line){  /*  es kam eine neue Zeile  */
  471.             memmove(s+1,s,MAXINPUT);
  472.             *s='(';
  473.             if(DEBUG&1) printf("look-ahead: %s|\n",s);
  474.             merk=s;
  475.             s++;
  476.             cpbez(buff,0);
  477.         }else{
  478.             if(DEBUG&1) printf("read_new_line unchanged\n");
  479.             cpbez(buff,0);
  480.         }
  481.     }else{
  482.         cpbez(buff,0);
  483.     }
  484.     s=merk;
  485.     if(!strcmp("auto",buff)) return(1);
  486.     if(!strcmp("char",buff)) return(1);
  487.     if(!strcmp("const",buff)) return(1);
  488.     if(!strcmp("double",buff)) return(1);
  489.     if(!strcmp("enum",buff)) return(1);
  490.     if(!strcmp("extern",buff)) return(1);
  491.     if(!strcmp("float",buff)) return(1);
  492.     if(!strcmp("int",buff)) return(1);
  493.     if(!strcmp("long",buff)) return(1);
  494.     if(!strcmp("register",buff)) return(1);
  495.     if(!strcmp("short",buff)) return(1);
  496.     if(!strcmp("signed",buff)) return(1);
  497.     if(!strcmp("static",buff)) return(1);
  498.     if(!strcmp("struct",buff)) return(1);
  499.     if(!strcmp("typedef",buff)) return(1);
  500.     if(!strcmp("union",buff)) return(1);
  501.     if(!strcmp("unsigned",buff)) return(1);
  502.     if(!strcmp("void",buff)) return(1);
  503.     if(!strcmp("volatile",buff)) return(1);
  504.     if(!(c_flags[7]&USEDFLAG)&&!strcmp("__reg",buff)) return(1);
  505.     v=find_var(buff,0);
  506.     if(v&&v->storage_class==TYPEDEF) return(1);
  507.     return(0);
  508. }
  509. void add_sl(struct struct_declaration *sd,struct struct_list (*sl)[])
  510. /*  Fuegt ein struct_list-Array in eine struct_declaration ein.     */
  511. /*  Das Array muss mind. sd->count Elements haben und wird kopiert. */
  512. {
  513.     size_t sz=sizeof(struct struct_list)*sd->count;
  514.     sd->sl=mymalloc(sz);
  515.     memcpy(sd->sl,sl,sz);
  516. }
  517. struct struct_declaration *add_sd(struct struct_declaration *new)
  518. /*  Fuegt eine struct Declaration in Liste ein      */
  519. {
  520.     new->next=0;
  521.     if(first_sd[nesting]==0){
  522.         first_sd[nesting]=last_sd[nesting]=new;
  523.     }else{
  524.         last_sd[nesting]->next=new;
  525.         last_sd[nesting]=new;
  526.     }
  527.     return(new);
  528. }
  529. void free_sd(struct struct_declaration *p)
  530. /*  Gibt eine struct_declaration-List inkl. struct_lists und    */
  531. /*  allen Typen jeder struct_list frei, nicht aber identifier   */
  532. {
  533.     int i;struct struct_declaration *merk;
  534.     while(p){
  535.         merk=p->next;
  536.         for(i=0;i<p->count;i++) if((*p->sl)[i].styp) freetyp((*p->sl)[i].styp);
  537.         if(p->count>0) free(p->sl);
  538.         free(p);
  539.         p=merk;
  540.     }
  541. }
  542. char *add_identifier(char *identifier,int length)
  543. /*  Kopiert identifier an sicheren Ort, der spaeter zentral     */
  544. /*  freigegeben werden kann.                                    */
  545. /*  Sollte noch einbauen, dass ueberprueft wird, ob schon       */
  546. /*  vorhanden und dann nicht zweimal speichern                  */
  547. {
  548.     struct identifier_list *new;
  549.     if((*identifier==0&&length==0)||identifier==empty) return(empty);
  550.     new=mymalloc(sizeof(struct identifier_list));
  551.     new->identifier=mymalloc(length+1);
  552.     memcpy(new->identifier,identifier,length+1);
  553.     new->next=0;new->length=length;
  554.     if(last_ilist[nesting]){
  555.         last_ilist[nesting]->next=new;
  556.         last_ilist[nesting]=new;
  557.     }else{
  558.         last_ilist[nesting]=first_ilist[nesting]=new;
  559.     }
  560.     return(new->identifier);
  561. }
  562. void free_ilist(struct identifier_list *p)
  563. /*  Gibt eine verkettete identifier_liste und saemtliche darin  */
  564. /*  gespeicherten Identifier frei.                              */
  565. {
  566.     struct identifier_list *merk;
  567.     while(p){
  568.         merk=p->next;
  569.         if(p->identifier) free(p->identifier);
  570.         free(p);
  571.         p=merk;
  572.     }
  573. }
  574. int type_uncomplete(struct Typ *p)
  575. /*  Testet, ob Typ unvollstaendig ist. Momentan gelten nur      */
  576. /*  unvollstaendige Strukturen und Arrays von solchen als       */
  577. /*  unvollstaendig, aber keine Zeiger oder Funktionen darauf    */
  578. {
  579.     struct struct_declaration *sd;
  580.     if(!p){ierror(0);return(0);}
  581.     if((p->flags&15)==STRUCT||(p->flags&15)==UNION)
  582.         if(p->exact->count<=0) return(1);
  583.     if((p->flags&15)==ARRAY){
  584.         if(zlleq(p->size,l2zl(0L))) return(1);
  585.         if(type_uncomplete(p->next)) return(1);
  586.     }
  587.     return(0);
  588. }
  589. void add_struct_identifier(char *identifier,struct struct_declaration *sd)
  590. /*  Erzeugt neuen struct_identifier, fuegt ihn in Liste an und  */
  591. /*  vervollstaendigt unvollstaendige Typen dieser Struktur.     */
  592. {
  593.     struct struct_identifier *new;
  594. /*    struct Typ *t;*/
  595.     if(DEBUG&1) printf("add_si %s (nesting=%d)->%p\n",identifier,nesting,(void *)sd);
  596.     new=mymalloc(sizeof(struct struct_identifier));
  597.     new->identifier=add_identifier(identifier,strlen(identifier));
  598.     new->sd=sd; new->next=0;
  599.     if(first_si[nesting]==0){
  600.         first_si[nesting]=new;last_si[nesting]=new;
  601.     }else{
  602.         last_si[nesting]->next=new;last_si[nesting]=new;
  603.     }
  604. }
  605. void free_si(struct struct_identifier *p)
  606. /*  Gibt eine struct_identifier-Liste frei, aber nicht die      */
  607. /*  identifiers und struct_declarations                         */
  608. {
  609.     struct struct_identifier *merk;
  610.     while(p){
  611.         merk=p->next;
  612.         free(p);
  613.         p=merk;
  614.     }
  615. }
  616. struct struct_declaration *find_struct(char *identifier,int endnesting)
  617. /*  Sucht angegebene Strukturdefinition und liefert             */
  618. /*  entsprechende struct_declaration                            */
  619. {
  620.     struct struct_identifier *si; int i;
  621.     for(i=nesting;i>=endnesting;i--){
  622.         si=first_si[i];
  623.         while(si){
  624.             if(!strcmp(si->identifier,identifier)){
  625.                 if(DEBUG&1) printf("found struct tag <%s> at nesting %d->%p\n",identifier,i,(void *)si->sd);
  626.                 return(si->sd);
  627.             }
  628.             si=si->next;
  629.         }
  630.     }
  631.     if(DEBUG&1) printf("didn't find struct tag <%s>\n",identifier);
  632.     return(0);
  633. }
  634. struct Var *add_var(char *identifier, struct Typ *t, int storage_class,struct const_list *clist)
  635. /*  Fuegt eine Variable mit Typ in die var_list ein             */
  636. /*  maschinenspezifisches und Codegeneration fehlen noch        */
  637. /*  Alignment maschinenabhaengig                                */
  638. /*  In der storage_class werden die Flags PARAMETER und evtl.   */
  639. /*  OLDSTYLE und REGPARM erkannt.                               */
  640. {
  641.     struct Var *new;int f;
  642.     struct struct_declaration *sd;
  643.     static zlong paroffset;
  644.     /*if(*identifier==0) return;*/ /* sollte woanders bemaekelt werden */
  645.     if(DEBUG&2) printf("add_var(): %s\n",identifier);
  646.     if((t->flags&15)==FUNKT&&((t->next->flags&15)==ARRAY||(t->next->flags&15)==FUNKT))
  647.         error(25);
  648.     new=mymalloc(sizeof(struct Var));
  649.     new->identifier=add_identifier(identifier,strlen(identifier));
  650.     new->clist=clist;
  651.     new->vtyp=t;
  652.     new->storage_class=storage_class&7;
  653.     new->reg=0;
  654.     new->next=0;
  655.     new->flags=0;
  656.     new->fi=0;
  657.     new->nesting=nesting;
  658. /*    if((storage_class&7)==STATIC||(storage_class&7)==EXTERN) new->flags=USEDASSOURCE|USEDASDEST;*/
  659.     if(DEBUG&2) printf("storage_class=%d\n",storage_class);
  660.     if(storage_class&PARAMETER) new->flags|=USEDASDEST;
  661.     if(storage_class®PARM) {new->flags|=REGPARM;storage_class&=~PARAMETER;}
  662.     if(DEBUG&2) printf("storage_class=%d\n",storage_class);
  663.     if(DEBUG&2) printf("max_offset=%ld\n",max_offset);
  664.     if((storage_class&7)==REGISTER) new->priority=registerpri; else new->priority=0;
  665.     if(last_var[nesting]){
  666.         new->offset=zladd(last_var[nesting]->offset,szof(last_var[nesting]->vtyp));
  667.         last_var[nesting]->next=new;
  668.         last_var[nesting]=new;
  669.     }else{
  670.         new->offset=l2zl(0L);
  671.         paroffset=maxalign;
  672.         first_var[nesting]=last_var[nesting]=new;
  673.     }
  674.     f=t->flags&15;
  675.     if((storage_class&7)==AUTO||(storage_class&7)==REGISTER){
  676.         if(DEBUG&2) printf("auto\n");
  677.         if(type_uncomplete(t)&&(t->flags&15)!=ARRAY) error(202,identifier);
  678.         /*  das noch ueberpruefen   */
  679.         if((c_flags_val[0].l&2)&&nesting==1&&!(storage_class&PARAMETER)){
  680.             new->offset=max_offset;
  681.         }else{
  682.             if(storage_class&PARAMETER){
  683.                 new->offset=paroffset;
  684.             }else{
  685.                 new->offset=local_offset[nesting];
  686.             }
  687.         }
  688.         new->offset=zlmult(zldiv(zladd(new->offset,zlsub(align[f],l2zl(1L))),align[f]),align[f]);
  689.         if(storage_class&PARAMETER){
  690.             if(f>=CHAR&&f<=SHORT){
  691.             /*  Integer-Erweiterungen fuer alle Funktionsparameter  */
  692.                 paroffset=zladd(new->offset,sizetab[INT]);
  693.             }else{
  694.                 paroffset=zladd(new->offset,szof(new->vtyp));
  695.             }
  696.         }else{
  697.             local_offset[nesting]=zladd(new->offset,szof(new->vtyp));
  698.         }
  699.         /*  Bei alten Funktionen werden FLOAT als DOUBLE uebergeben */
  700.         if((storage_class&(PARAMETER|OLDSTYLE))==(PARAMETER|OLDSTYLE)&&f==FLOAT)
  701.             paroffset=zladd(local_offset[nesting],zlsub(sizetab[DOUBLE],sizetab[FLOAT]));
  702.  
  703.         if(!(storage_class&PARAMETER))
  704.             if(zlleq(max_offset,local_offset[nesting])) max_offset=local_offset[nesting];
  705.         if(DEBUG&2) printf("max_offset=%ld\n",max_offset);
  706.     }
  707.     if((storage_class&7)==STATIC) new->offset=l2zl((long)++label);
  708.     if(storage_class&PARAMETER){
  709. /*        new->storage_class&=~PARAMETER;*/
  710.         /* ob das hier so funktioniert ? Bei BIGENDIAN nimmt man den    */
  711.         /* hinteren Teil des INTs, bei LOWENDIAN muesste man eigentlich */
  712.         /* nichts tun, oder? Datenformate, bei denen wirklich           */
  713.         /* umgewandelt werden muss, werden (noch?) nicht unterstuetzt.  */
  714.         /* Ob sowas aber ueberhaupt zulaessig waere, weiss ich nicht.   */
  715.  
  716.         if(DEBUG&2) printf("parameter\n");
  717.  
  718.         if(f>=CHAR&&f<=SHORT&&!zlleq(sizetab[INT],sizetab[f])){
  719.             if(BIGENDIAN){
  720.                 new->offset=zladd(new->offset,zlsub(sizetab[INT],sizetab[f]));
  721.             }else{
  722.                 if(!LITTLEENDIAN)
  723.                     ierror(0);
  724.             }
  725.         }
  726.         if((storage_class&OLDSTYLE)&&f==FLOAT){
  727.         /*  Bei alten Funktionen werden DOUBLE nach FLOAT konvertiert   */
  728.             struct IC *conv=mymalloc(ICS);
  729.             conv->code=CONVDOUBLE;
  730.             conv->typf=FLOAT;
  731.             conv->q1.flags=VAR|DONTREGISTERIZE;
  732.             conv->z.flags=VAR;
  733.             conv->q2.flags=0;
  734.             conv->q1.v=conv->z.v=new;
  735.             conv->q1.val.vlong=conv->z.val.vlong=l2zl(0);
  736.             add_IC(conv);
  737.             new->flags|=CONVPARAMETER;
  738.         }
  739.         new->offset=zlsub(l2zl(0L),new->offset);
  740.     }
  741.     if((storage_class&7)==EXTERN){
  742.         if(!strcmp("fprintf",identifier)) new->flags|=PRINTFLIKE;
  743.         if(!strcmp("printf",identifier))  new->flags|=PRINTFLIKE;
  744.         if(!strcmp("sprintf",identifier)) new->flags|=PRINTFLIKE;
  745.         if(!strcmp("fscanf",identifier))  new->flags|=SCANFLIKE;
  746.         if(!strcmp("scanf",identifier))   new->flags|=SCANFLIKE;
  747.         if(!strcmp("sscanf",identifier))  new->flags|=SCANFLIKE;
  748.     }
  749.     return(new);
  750. }
  751. void free_fi(struct function_info *p)
  752. /*  Gibt ein function_info mit Inhalt frei  */
  753. {
  754.     if(p->first_ic) free_IC(p->first_ic);
  755.     if(p->vars) free_var(p->vars);
  756.     free(p);
  757. }
  758. void free_var(struct Var *p)
  759. /*  Gibt Variablenliste inkl. Typ, aber ohne Identifier frei    */
  760. {
  761.     struct Var *merk;
  762.     while(p){
  763.         merk=p->next;
  764.         if(!(p->flags&USEDASADR)&&(p->storage_class==AUTO||p->storage_class==REGISTER)){
  765.             if(*p->identifier&&!(p->flags&USEDASDEST)&&(p->vtyp->flags&15)<=POINTER) error(64,p->identifier);
  766.             if(*p->identifier&&!(p->flags&USEDASSOURCE)&&(p->vtyp->flags&15)<=POINTER) error(65,p->identifier);
  767.         }
  768.         if(DEBUG&2) printf("free_var %s, pri=%d\n",p->identifier,p->priority);
  769.         if(p->vtyp) freetyp(p->vtyp);
  770.         if(p->clist) free_clist(p->clist);
  771.         if(p->fi){
  772.             if(DEBUG&2) printf("free_fi of function %s\n",p->identifier);
  773.             free_fi(p->fi);
  774.             if(DEBUG&2) printf("end free_fi of function %s\n",p->identifier);
  775.         }
  776.         free(p);
  777.         p=merk;
  778.     }
  779. }
  780. struct Var *find_var(char *identifier,int endnesting)
  781. /*  sucht Variable mit Bezeichner und liefert Zeiger zurueck    */
  782. /*  es werden nur Variablen der Bloecke endnesting-nesting      */
  783. /*  durchsucht                                                  */
  784. {
  785.     int i;struct Var *v;
  786.     if(*identifier==0||identifier==0) return(0);
  787.     for(i=nesting;i>=endnesting;i--){
  788.         v=first_var[i];
  789.         while(v){
  790.             if(!strcmp(v->identifier,identifier)) return(v);
  791.             v=v->next;
  792.         }
  793.     }
  794.     return(0);
  795. }
  796. void var_declaration(void)
  797. /*  Bearbeitet eine Variablendeklaration und erzeugt alle       */
  798. /*  noetigen Strukturen                                         */
  799. {
  800.     struct Typ *ts,*t,*old=0,*om=0;char *imerk,vident[MAXI];
  801.     int mdef=0,makeint=0,notdone,storage_class,msc,extern_flag,isfunc,
  802.         had_decl,hard_reg,mhr;
  803.     struct Var *v;
  804.     ts=declaration_specifiers();notdone=1;
  805.     storage_class=return_sc;hard_reg=return_reg;
  806.     if(storage_class==EXTERN) extern_flag=1; else extern_flag=0;
  807.     killsp();
  808.     if(*s==';'){
  809.         if(storage_class||((ts->flags&15)!=STRUCT&&(ts->flags&15)!=UNION&&(ts->flags&15)!=INT))
  810.             error(36);
  811.         freetyp(ts);s++;killsp();
  812.         return;
  813.     }
  814.     if(nesting==0&&(storage_class==AUTO||storage_class==REGISTER))
  815.         {error(66);storage_class=EXTERN;}
  816.     if(!ts){
  817.         if(nesting<=1){
  818.             ts=mymalloc(TYPS);
  819.             ts->flags=INT;ts->next=0;
  820.             makeint=1;
  821.             if(!storage_class) storage_class=EXTERN;
  822.             error(67);
  823.         }else{
  824.             ierror(0);return;
  825.         }
  826.     }
  827.     if(storage_class==0){
  828.         if(nesting==0) storage_class=EXTERN; else storage_class=AUTO;
  829.     }
  830.     msc=storage_class;mhr=hard_reg;
  831.     while(notdone){
  832.         int oldnesting=nesting;
  833.         imerk=ident;ident=vident;*vident=0;  /* merken von ident hier vermutlich */
  834.         storage_class=msc;hard_reg=mhr;
  835.         if(old) {freetyp(old);old=0;}
  836.         t=declarator(clone_typ(ts));
  837.         if((t->flags&15)!=FUNKT) isfunc=0;
  838.             else {isfunc=1;if(storage_class!=STATIC) storage_class=EXTERN;}
  839.         ident=imerk;                    /* nicht unbedingt noetig ?         */
  840.         v=find_var(vident,oldnesting);
  841.         if(v){
  842.             had_decl=1;
  843.             if(nesting>0&&(v->flags&DEFINED)&&!extern_flag&&!isfunc){
  844.                 error(27,vident);
  845.             }else{
  846.                 if(t&&v->vtyp&&!compare_pointers(v->vtyp,t,255)){
  847.                     error(68,vident);
  848.                 }
  849.                 if((storage_class!=v->storage_class&&!extern_flag)||hard_reg!=v->reg)
  850.                     error(28,v->identifier);
  851.                 if(!isfunc&&!extern_flag) v->flags|=TENTATIVE;
  852.             }
  853.             if(!isfunc){
  854.                 v->vtyp=t;
  855.             }else{
  856.                 om=v->vtyp;
  857.                 if(t->exact->count>0) {old=v->vtyp;v->vtyp=t;}
  858.             }
  859.         }else{
  860.             had_decl=0;
  861.             if(isfunc&&*s!=','&&*s!=';'&&*s!=')'&&*s!='='&&nesting>0) nesting--;
  862.             v=add_var(vident,t,storage_class,0);
  863.             v->reg=hard_reg;
  864.             if(isfunc&&*s!=','&&*s!=';'&&*s!=')'&&*s!='='&&nesting>=0) nesting++;
  865.             if(!v) ierror(0);
  866.             else{
  867.                 if(!isfunc&&!extern_flag){
  868.                     v->flags|=TENTATIVE;
  869.                     if(nesting>0) v->flags|=DEFINED;
  870.                 }
  871.             }
  872.             om=0;
  873.         }
  874.         killsp();
  875.         /*  Initialisierung von Variablen bei Deklaration   */
  876.         if(*s=='='){
  877.             s++;killsp();
  878.             if(!had_decl&&v->nesting==0&&v->storage_class==EXTERN)
  879.                 error(168,v->identifier);
  880.             if(v->flags&DEFINED) {if(nesting==0) error(30,v->identifier);}
  881.                 else v->flags|=DEFINED;
  882.             if(v->storage_class==TYPEDEF) error(114,v->identifier);
  883.             if(extern_flag){
  884.                 if(nesting==0)
  885.                     error(118,v->identifier);
  886.                 else
  887.                     error(207,v->identifier);
  888.                 if(v->storage_class!=EXTERN){ error(77);v->storage_class=EXTERN;}
  889.             }
  890.             v->clist=initialization(v->vtyp,v->storage_class==AUTO||v->storage_class==REGISTER);
  891.             if(v->clist){
  892.                 if((v->vtyp->flags&15)==ARRAY&&zleqto(v->vtyp->size,l2zl(0L))){
  893.                     struct const_list *p=v->clist;
  894.                     while(p){v->vtyp->size=zladd(v->vtyp->size,l2zl(1L));p=p->next;}
  895.                     if(v->storage_class==AUTO||v->storage_class==REGISTER){
  896.                         local_offset[nesting]=zladd(local_offset[nesting],szof(v->vtyp));
  897.                         if(zlleq(max_offset,local_offset[nesting])) max_offset=local_offset[nesting];
  898.                     }
  899.                 }
  900.                 if(v->storage_class==AUTO||v->storage_class==REGISTER){
  901.                     struct IC *new;
  902.                 /*  Initialisierung von auto-Variablen  */
  903.                     new=mymalloc(ICS);
  904.                     new->code=ASSIGN;
  905.                     new->typf=v->vtyp->flags;
  906.                     new->q2.flags=0;
  907.                     new->q2.val.vlong=szof(v->vtyp);
  908.                     new->z.flags=VAR;
  909.                     new->z.v=v;
  910.                     new->z.val.vlong=l2zl(0L);
  911.                     if(v->clist->tree){
  912.                     /*  einzelner Ausdruck  */
  913.                         gen_IC(v->clist->tree,0,0);
  914.                         convert(v->clist->tree,v->vtyp->flags&31);
  915.                         new->q1=v->clist->tree->o;
  916. /*                        v->clist=0;*/
  917.                     }else{
  918.                     /*  Array etc.  */
  919.                         struct Var *nv;
  920.                         nv=add_var(empty,clone_typ(v->vtyp),STATIC,v->clist);
  921.                         nv->flags|=DEFINED;
  922.                         nv->vtyp->flags|=CONST;
  923. /*                        v->clist=0;*/
  924.                         new->q1.flags=VAR;
  925.                         new->q1.v=nv;
  926.                         new->q1.val.vlong=l2zl(0L);
  927.                     }
  928.                     add_IC(new);
  929. /*                    if(v->clist&&v->clist->tree){free_expression(v->clist->tree);v->clist->tree=0;}*/
  930.                 }else if(c_flags[19]&USEDFLAG){
  931.                     /*  Ohne Optimierung gleich erzeugen; das ist noch  */
  932.                     /*  etwas von der genauen Implementierung der Liste */
  933.                     /*  der Variablen abhaengig.                        */
  934.                     struct Var *merk=v->next;
  935.                     v->next=0;
  936.                     gen_vars(v);
  937.                     v->next=merk;
  938.                     v->clist=0;
  939.                 }
  940.             }
  941.         }else{
  942.             if((v->flags&DEFINED)&&type_uncomplete(v->vtyp)) error(202,v->identifier);
  943.             if((v->vtyp->flags&CONST)&&(v->storage_class==AUTO||v->storage_class==REGISTER))
  944.                 error(119,v->identifier);
  945.         }
  946.         if(*s==',') {s++;killsp();mdef=1;} else notdone=0;
  947.     }
  948.     freetyp(ts);
  949.     if(!mdef&&t&&(t->flags&15)==FUNKT&&*s!=';'){
  950.     /*  Funktionsdefinition                                     */
  951.         int i,oldstyle=0;
  952.         fline=line;
  953.         if(DEBUG&1) printf("Funktionsdefinition!\n");
  954.         {int i;
  955.             for(i=1;i<=MAXR;i++) {regs[i]=regused[i]=regsa[i];regsbuf[i]=0;}
  956.         }
  957.         cur_func=v->identifier;
  958.         if(only_inline==2) only_inline=0;
  959.         if(nesting<1) ierror(0);
  960.         if(nesting>1) error(32);
  961.         if(v->flags&DEFINED) error(33,v->identifier);
  962.             else v->flags|=DEFINED;
  963.         if(storage_class!=EXTERN&&storage_class!=STATIC) error(34);
  964.         if(extern_flag) error(120);
  965.         if(storage_class==EXTERN&&!strcmp(v->identifier,"main")&&(!t->next||t->next->flags!=INT)) error(121);
  966.         if(!had_decl&&v->nesting==0&&v->storage_class==EXTERN)
  967.             error(168,v->identifier);
  968.         while(*s!='{'){
  969.         /*  alter Stil  */
  970.             struct Typ *nt=declaration_specifiers();notdone=1;oldstyle=OLDSTYLE;
  971.             if(!ts) {error(35);}
  972.             while(notdone){
  973.                 int found=0;
  974.                 imerk=ident;ident=vident;*vident=0;
  975.                 ts=declarator(clone_typ(nt));
  976.                 ident=imerk;
  977.                 if(!ts) {error(36);}
  978.                 else{
  979.                     for(i=0;i<t->exact->count;i++){
  980.                         if(!strcmp((*t->exact->sl)[i].identifier,vident)){
  981.                             found=1;
  982.                             if((*t->exact->sl)[i].styp){
  983.                                 error(69,vident);
  984.                                 freetyp((*t->exact->sl)[i].styp);
  985.                             }
  986.                             /*  typ[] in *typ   */
  987.                             if((ts->flags&15)==ARRAY) ts->flags=POINTER;
  988.                             /*  typ() in *typ() */
  989.                             if((ts->flags&15)==FUNKT){
  990.                                 struct Typ *new=mymalloc(TYPS);
  991.                                 new->flags=POINTER;
  992.                                 new->next=ts;
  993.                                 ts=new;
  994.                             }
  995.                             if(!return_sc) return_sc=AUTO;
  996.                             if(return_sc!=AUTO&&return_sc!=REGISTER)
  997.                                 {error(122);return_sc=AUTO;}
  998.                             (*t->exact->sl)[i].storage_class=return_sc;
  999.                             (*t->exact->sl)[i].reg=return_reg;
  1000.                             if(return_reg) error(219);
  1001.                             (*t->exact->sl)[i].styp=ts;
  1002.                         }
  1003.                     }
  1004.                 }
  1005.                 if(!found) {error(37,vident);}
  1006.                 killsp();
  1007.                 if(*s==',') {s++;killsp();} else notdone=0;
  1008.             }
  1009.             if(nt) freetyp(nt);
  1010.             if(*s==';'){s++;killsp();
  1011.             }else{
  1012.                 error(54);
  1013.                 while(*s!='{'&&*s!=';'){s++;killsp();}
  1014.             }
  1015.         }
  1016.         if(t->exact->count==0){
  1017.             struct struct_list sl[1];
  1018.             if(DEBUG&1) printf("prototype converted to (void)\n");
  1019.             t->exact->count=1;
  1020.             sl[0].identifier=empty;
  1021.             sl[0].storage_class=AUTO;
  1022.             sl[0].styp=mymalloc(TYPS);
  1023.             sl[0].styp->flags=VOID;
  1024.             sl[0].styp->next=0;
  1025.             nesting--;
  1026.             add_sl(t->exact,&sl);
  1027.             nesting++;
  1028.         }
  1029.         if(om&&!compare_sd(om->exact,t->exact))
  1030.             error(123);
  1031.         nocode=0;currentpri=1;
  1032. /*        enter_block();*/
  1033.         local_offset[1]=l2zl(0L);
  1034.         return_var=0;
  1035.         if(!v->vtyp) ierror(0);
  1036.         if(v->vtyp->next->flags==VOID) return_typ=0;
  1037.         else{
  1038.             return_typ=v->vtyp->next;
  1039.             if(!freturn(return_typ)){
  1040.                 /*  Parameter fuer die Rueckgabe von Werten, die nich in einem  */
  1041.                 /*  Register sind.                                              */
  1042.                 struct Typ *rt=mymalloc(TYPS);
  1043.                 rt->flags=POINTER;rt->next=return_typ;
  1044.                 return_var=add_var(empty,clone_typ(rt),AUTO|PARAMETER|oldstyle,0);
  1045.                 return_var->flags|=DEFINED;
  1046.                 free(rt);
  1047.             }
  1048.         }
  1049.         first_ic=last_ic=0;ic_count=0;max_offset=l2zl(0L);
  1050.         for(i=0;i<t->exact->count;i++){
  1051.             if(!(*t->exact->sl)[i].styp&&*(*t->exact->sl)[i].identifier){
  1052.                 struct Typ *nt;
  1053.                 nt=mymalloc(TYPS);
  1054.                 nt->flags=INT; nt->next=0;
  1055.                 (*t->exact->sl)[i].styp=nt;
  1056.                 (*t->exact->sl)[i].storage_class=AUTO;
  1057.                 (*t->exact->sl)[i].reg=0;
  1058.                 error(124);
  1059.             }
  1060.             if(*(*t->exact->sl)[i].identifier){
  1061.                 struct Var *tmp;int sc;
  1062.                 sc=((*t->exact->sl)[i].storage_class|PARAMETER|oldstyle);
  1063.                 if((*t->exact->sl)[i].reg) sc|=REGPARM;
  1064.                 tmp=add_var((*t->exact->sl)[i].identifier,clone_typ((*t->exact->sl)[i].styp),sc,0);
  1065.                 tmp->reg=(*t->exact->sl)[i].reg;
  1066.                 tmp->flags|=DEFINED;
  1067.                 if(oldstyle){
  1068.                     freetyp((*t->exact->sl)[i].styp);
  1069.                     (*t->exact->sl)[i].styp=0; /*  Prototype entfernen */
  1070.                 }
  1071.             }
  1072.         }
  1073.         if(oldstyle) t->exact->count=0; /*  Prototype entfernen */
  1074. /*        local_offset[1]=l2zl(0L);*/
  1075.         return_label=++label;
  1076.         v->flags|=GENERATED;
  1077.         function_calls=0;float_used=0;has_return=0;goto_used=0;
  1078.         compound_statement();
  1079.         if((v->vtyp->next->flags&15)!=VOID&&!has_return){
  1080.             if(strcmp(v->identifier,"main")) error(173,v->identifier);
  1081.                 else error(174,v->identifier);
  1082.         }
  1083. #if 0
  1084.         {int i;
  1085.             for(i=1;i<=MAXR;i++) if(regs[i]!=regsa[i]) {printf("Register %s:\n",regnames[i]);ierror(0);}
  1086.         }
  1087. #endif
  1088.         gen_label(return_label);
  1089.         if(first_ic&&errors==0){
  1090.             if((c_flags[2]&USEDFLAG)&&ic1){fprintf(ic1,"function %s\n",v->identifier); pric(ic1,first_ic);}
  1091.             optimize(c_flags_val[0].l,v);
  1092.             if((c_flags[3]&USEDFLAG)&&ic2){fprintf(ic2,"function %s\n",v->identifier); pric(ic2,first_ic);}
  1093.             if(out&&!only_inline&&!(c_flags[5]&USEDFLAG)){
  1094.                 gen_code(out,first_ic,v,max_offset);
  1095.             }
  1096. /*            if(DEBUG&8192){fprintf(ic2,"function %s, after gen_code\n",v->identifier); pric(ic2,first_ic);}*/
  1097.             free_IC(first_ic);
  1098.             first_ic=last_ic=0;
  1099.         }
  1100.         if(v->fi&&v->fi->first_ic){
  1101.             struct Var *vp;
  1102.             if(DEBUG&1) printf("leave block %d (inline-version)\n",nesting);
  1103.             if(nesting!=1) ierror(0);
  1104.             if(merk_varl) merk_varl->next=first_var[nesting]; else merk_varf=first_var[nesting];
  1105.             if(last_var[nesting]) merk_varl=last_var[nesting];
  1106.             if(merk_sil) merk_sil->next=first_si[nesting]; else merk_sif=first_si[nesting];
  1107.             if(last_si[nesting]) merk_sil=last_si[nesting];
  1108.             if(merk_sdl) merk_sdl->next=first_sd[nesting]; else merk_sdf=first_sd[nesting];
  1109.             if(last_sd[nesting]) merk_sdl=last_sd[nesting];
  1110.             if(merk_ilistl) merk_ilistl->next=first_ilist[nesting]; else merk_ilistf=first_ilist[nesting];
  1111.             if(last_ilist[nesting]) merk_ilistl=last_ilist[nesting];
  1112.  
  1113.             if(merk_varf&&!only_inline) gen_vars(merk_varf);
  1114.             if(first_llist) free_llist(first_llist);
  1115.             if(first_clist) free_clist(first_clist);
  1116.             if(merk_sif) free_si(merk_sif);
  1117. /*  struct-declarations erst ganz am Schluss loeschen. Um zu vermeiden,     */
  1118. /*  dass struct-declarations in Prototypen frei werden und dann eine        */
  1119. /*  spaetere struct, dieselbe Adresse bekommt und dadurch gleich wird.      */
  1120. /*  Nicht sehr schoen - wenn moeglich noch mal aendern.                     */
  1121. /*            if(merk_sdf) free_sd(merk_sdf);*/
  1122.             /*  hier noch was ueberlegen    */
  1123. /*            if(merk_ilistf) free_ilist(merk_ilistf);*/
  1124.             nesting--;
  1125.             v->fi->vars=merk_varf;
  1126. /*            v->fi->vars=first_var[1];*/
  1127.             /*  keine echten Parameter=>keine negativen Offsets */
  1128. /*            vp=first_var[1];*/
  1129.             vp=merk_varf;
  1130.             while(vp){
  1131.                 if(vp->storage_class==AUTO||vp->storage_class==REGISTER){
  1132.                     if(!zlleq(l2zl(0L),vp->offset)){
  1133.                         vp->offset=l2zl(0L);
  1134.                         if(DEBUG&1024) printf("converted parameter <%s>(%ld) for inlining\n",vp->identifier,(long)zl2l(vp->offset));
  1135.                     }else vp->offset=l2zl(4L);  /*  Dummy, da recalc_offsets?   */
  1136.                 }
  1137.                 vp=vp->next;
  1138.             }
  1139.         }else{
  1140.             leave_block();
  1141.         }
  1142.         if(only_inline==2) only_inline=0;
  1143.         cur_func="oops, I forgot it";
  1144.     }else{
  1145.         if(makeint) error(125);
  1146.         if(*s==';') s++; else error(54);
  1147.         if((t->flags&15)==FUNKT&&t->exact){
  1148.             struct struct_declaration *sd=t->exact;int i,f;
  1149.             for(f=0,i=0;i<sd->count;i++)
  1150.                 if(!(*sd->sl)[i].styp){error(126);f=1;}
  1151.             if(f){
  1152.                 for(i=0;i<sd->count;i++) if((*sd->sl)[i].styp) freetyp((*sd->sl)[i].styp);
  1153.                 sd->count=0;
  1154.             }
  1155.         }
  1156.     }
  1157.     if(old) freetyp(old);
  1158. }
  1159. struct Typ *clone_typ(struct Typ *old)
  1160. /*  Erzeugt Kopie eines Typs und liefert Zeiger auf Kopie   */
  1161. {
  1162.     struct Typ *new;
  1163.     if(!old) return(0);
  1164.     new=mymalloc(TYPS);
  1165.     *new=*old;
  1166.     if(new->next) new->next=clone_typ(new->next);
  1167.     return(new);
  1168. }
  1169. int compare_pointers(struct Typ *a,struct Typ *b,int qual)
  1170. /*  vergleicht, ob Typ beider Typen gleich ist, const/volatile      */
  1171. /*  werden laut ANSI nicht beruecksichtigt                          */
  1172. {
  1173.     struct struct_declaration *sd;
  1174.     int af=a->flags&qual,bf=b->flags&qual;
  1175.     if(af!=bf) return(0);
  1176.     af&=15;bf&=15;
  1177.     if(af==FUNKT){
  1178.         if(a->exact->count&&!compare_sd(a->exact,b->exact)) return(0);
  1179.     }
  1180.     if(af==STRUCT||af==UNION){
  1181.         if(a->exact!=b->exact) return(0);
  1182.     }
  1183.     if(af==ARRAY){
  1184.         if(!zleqto(a->size,l2zl(0L))&&!zleqto(b->size,l2zl(0L))&&!zleqto(a->size,b->size)) return(0);
  1185.     }
  1186.     if(a->next==0&&b->next!=0) return(0);
  1187.     if(a->next!=0&&b->next==0) return(0);
  1188.     if(a->next==0&&b->next==0) return(1);
  1189.     return(compare_pointers(a->next,b->next,qual));
  1190. }
  1191. int compare_sd(struct struct_declaration *a,struct struct_declaration *b)
  1192. /*  Vergleicht, ob zwei struct_declarations identisch sind          */
  1193. /*  Wird nur nur fuer Prototypen benutzt, leere Liste immer gleich  */
  1194. {
  1195.     int i;
  1196.     if(!a->count||!b->count) return(1);
  1197.     if(a->count!=b->count) return(0);
  1198.     for(i=0;i<a->count;i++)
  1199.         if((*a->sl)[i].styp&&(*b->sl)[i].styp&&!compare_pointers((*a->sl)[i].styp,(*b->sl)[i].styp,255)) return(0);
  1200.     return(1);
  1201. }
  1202. void free_clist(struct const_list *p)
  1203. /*  gibt clist frei                                         */
  1204. {
  1205.     struct const_list *merk;
  1206.     return;
  1207.     while(p){
  1208.         merk=p->next;
  1209.         if(p->other) free_clist(p->other);
  1210.         if(p->tree) free_expression(p->tree);
  1211.         free(p);
  1212.         p=merk;
  1213.     }
  1214. }
  1215. void gen_clist(FILE *,struct Typ *,struct const_list *);
  1216.  
  1217. void gen_vars(struct Var *v)
  1218. /*  generiert Variablen                                     */
  1219. {
  1220.     int mode,al;struct Var *p;
  1221.     if(errors!=0||(c_flags[5]&USEDFLAG)) return;
  1222.     for(mode=0;mode<3;mode++){
  1223.         for(al=maxalign;al>=1;al--){
  1224.             int i,flag;
  1225.             for(i=1,flag=0;i<15;i++) if(align[i]==al) flag=1;
  1226.             if(!flag) continue;
  1227.             gen_align(out,al);
  1228.             for(p=v;p;p=p->next){
  1229.                 if(DEBUG&2) printf("gen_var(): %s\n",p->identifier);
  1230.                 if(p->storage_class==STATIC||p->storage_class==EXTERN){
  1231.                     if(!(p->flags&GENERATED)){
  1232.                         if(p->storage_class==EXTERN&&!(p->flags&(USEDASSOURCE|USEDASDEST))&&!(p->flags&(TENTATIVE|DEFINED))) continue;
  1233.                         if((p->vtyp->flags&15)!=ARRAY){
  1234.                             if(align[p->vtyp->flags&15]!=al) continue;
  1235.                         }else{
  1236.                             if(align[p->vtyp->next->flags&15]!=al) continue;
  1237.                         }
  1238.                         /*  erst konstante initialisierte Daten */
  1239.                         if(mode==0){
  1240.                             if(!p->clist) continue;
  1241.                             if(!(p->vtyp->flags&(CONST|STRINGCONST))){
  1242.                                 struct Typ *t=p->vtyp;int f=0;
  1243.                                 do{
  1244.                                     if(t->flags&(CONST|STRINGCONST)) break;
  1245.                                     if((t->flags&15)!=ARRAY){f=1;break;}
  1246.                                     t=t->next;
  1247.                                 }while(1);
  1248.                                 if(f) continue;
  1249.                             }
  1250.                         }
  1251.                         /*  dann initiolisierte */
  1252.                         if(mode==1&&!p->clist) continue;
  1253.                         /*  und dann der Rest   */
  1254.                         if(mode==2&&p->clist) continue;
  1255.                         gen_var_head(out,p);
  1256.                         if(!(p->flags&(TENTATIVE|DEFINED))){
  1257.                             if(p->storage_class==STATIC) error(127,p->identifier);
  1258.                             continue;
  1259.                         }
  1260.                         if(!p->clist){
  1261.                             if(type_uncomplete(p->vtyp)) error(202,p->identifier);
  1262.                             gen_ds(out,szof(p->vtyp),p->vtyp);
  1263.                         }else{
  1264.                             gen_clist(out,p->vtyp,p->clist);
  1265.                         }
  1266.                         p->flags|=GENERATED;
  1267.                     }
  1268.                 }
  1269.             }
  1270.         }
  1271.     }
  1272. }
  1273. void gen_clist(FILE *f,struct Typ *t,struct const_list *cl)
  1274. /*  generiert dc fuer const_list                            */
  1275. /*  hier ist noch einiges zu tun                            */
  1276. {
  1277.     int i;zlong sz;
  1278.     if((t->flags&15)==ARRAY){
  1279.         for(sz=l2zl(0L);!zlleq(t->size,sz)&&cl;sz=zladd(sz,l2zl(1L)),cl=cl->next){
  1280.             if(!cl->other){ierror(0);return;}
  1281.             gen_clist(f,t->next,cl->other);
  1282.         }
  1283.         if(!zlleq(t->size,sz)) gen_ds(f,zlmult(zlsub(t->size,sz),szof(t->next)),t->next);
  1284.         return;
  1285.     }
  1286.     if((t->flags&15)==UNION){
  1287.         gen_clist(f,(*t->exact->sl)[0].styp,cl);
  1288.         sz=zlsub(szof(t),szof((*t->exact->sl)[0].styp));
  1289.         if(!zleqto(sz,l2zl(0L))) gen_ds(f,sz,0);
  1290.         return;
  1291.     }
  1292.     if((t->flags&15)==STRUCT){
  1293.         zlong al;int fl;struct Typ *st,*h;
  1294.         sz=l2zl(0L);
  1295.         for(i=0;i<t->exact->count&&cl;i++){
  1296.             if(!cl->other){ierror(0);return;}
  1297.             st=(*t->exact->sl)[i].styp;
  1298.             h=st;
  1299.             do{
  1300.                 fl=h->flags&15;
  1301.                 h=h->next;
  1302.             }while(fl==ARRAY);
  1303.             al=align[fl];
  1304.             if(!zleqto(zlmod(sz,al),l2zl(0L))){
  1305.                 gen_ds(f,zlsub(al,zlmod(sz,al)),0);
  1306.                 sz=zladd(sz,zlsub(al,zlmod(sz,al)));
  1307.             }
  1308.             if(!(*t->exact->sl)[i].identifier) ierror(0);
  1309.             if((*t->exact->sl)[i].identifier[0]){
  1310.                 gen_clist(f,st,cl->other);
  1311.                 cl=cl->next;
  1312.             }else{
  1313.                 gen_ds(f,szof(st),0); /* sollte unnamed bitfield sein */
  1314.             }
  1315.             sz=zladd(sz,szof(st));
  1316.         }
  1317.         for(;i<t->exact->count;i++){
  1318.             st=(*t->exact->sl)[i].styp;
  1319.             h=st;
  1320.             do{
  1321.                 fl=h->flags&15;
  1322.                 h=h->next;
  1323.             }while(fl==ARRAY);
  1324.             al=align[fl];
  1325.             if(!zleqto(zlmod(sz,al),l2zl(0L))){
  1326.                 gen_ds(f,zlsub(al,zlmod(sz,al)),0);
  1327.                 sz+=zladd(sz,zlsub(al,zlmod(sz,al)));
  1328.             }
  1329.             gen_ds(f,szof((*t->exact->sl)[i].styp),(*t->exact->sl)[i].styp);
  1330.             sz=zladd(sz,szof(st));
  1331.         }
  1332.         al=align[STRUCT];
  1333.         if(!zleqto(zlmod(sz,al),l2zl(0L)))
  1334.             gen_ds(f,zlsub(al,zlmod(sz,al)),0);
  1335.         return;
  1336.     }
  1337.     gen_dc(f,t->flags&31,cl);
  1338. }
  1339. struct const_list *initialization(struct Typ *t,int noconst)
  1340. /*  traegt eine Initialisierung in eine const_list ein          */
  1341. {
  1342.     struct const_list *first,*cl,**prev;np tree,tree2;int bracket;zlong i;
  1343.     int f=t->flags&15;
  1344.     if(f==FUNKT){error(42);return(0);}
  1345.     if(*s=='{'){s++;killsp();bracket=1;} else bracket=0;
  1346.     if(f==ARRAY){
  1347.         if(*s=='\"'&&t->next&&(t->next->flags&15)==CHAR){
  1348.             killsp();
  1349.             tree=string_expression();
  1350.             first=(struct const_list *)tree->identifier;
  1351.             free_expression(tree);
  1352.         }else{
  1353.             prev=0;
  1354.             for(i=l2zl(0L);(zleqto(t->size,l2zl(0L))||!zlleq(t->size,i))&&*s!='}';i=zladd(i,l2zl(1L))){
  1355.                 if(!zlleq(i,0)){
  1356.                     if(*s==','){s++;killsp();} else break;
  1357.                     if(*s=='}') break;
  1358.                 }
  1359.                 cl=mymalloc(CLS);
  1360.                 cl->next=0;cl->tree=0;
  1361.                 cl->other=initialization(t->next,0);
  1362.                 killsp();
  1363.                 if(prev) *prev=cl; else first=cl;
  1364.                 prev=&cl->next;
  1365.             }
  1366.         }
  1367.     }else if(f==STRUCT&&(bracket||!noconst)){
  1368.         if(t->exact->count<=0)
  1369.             {error(43);return(0);}
  1370.         prev=0;
  1371.         for(i=l2zl(0L);!zlleq(t->exact->count,i)&&*s!='}';i=zladd(i,l2zl(1L))){
  1372.             if((*t->exact->sl)[zl2l(i)].identifier[0]==0) {continue;} /* unnamed bitfield */
  1373.             if(!zlleq(i,0)){
  1374.                 if(*s==','){s++;killsp();} else break;
  1375.                 if(*s=='}') break;
  1376.             }
  1377.             cl=mymalloc(CLS);
  1378.             cl->next=0;cl->tree=0;
  1379.             cl->other=initialization((*t->exact->sl)[zl2l(i)].styp,0);
  1380.             if(prev) *prev=cl; else first=cl;
  1381.             prev=&cl->next;
  1382.         }
  1383.     }else if(f==UNION&&(bracket||!noconst)){
  1384.         if(t->exact->count<=0)
  1385.             {error(44);return(0);}
  1386.         first=initialization((*t->exact->sl)[0].styp,0);
  1387.     }else{
  1388.         tree2=tree=assignment_expression();
  1389.         if(!tree){error(45);return(0);}
  1390.         if(!type_expression(tree)){free_expression(tree); return(0);}
  1391.         tree=makepointer(tree);
  1392.         test_assignment(t,tree);
  1393.         if(!noconst){
  1394.         /*  nur Konstanten erlaubt (bei Arrays/Strukturen etc. oder static) */
  1395.             if(tree->flags!=CEXPR){
  1396.                 while(tree->flags==CAST) tree=tree->left;
  1397.                 if(tree->flags==ADDRESS||tree->flags==ADDRESSS||tree->flags==ADDRESSA){
  1398.                     gen_IC(tree,0,0);
  1399.                     if(!(tree->o.flags&VARADR)){
  1400.                     /*  hier fehlen noch viele Pruefungen   */
  1401.                         free_expression(tree);error(46);
  1402.                         return(0);
  1403.                     }
  1404.                     first=mymalloc(CLS);
  1405.                     first->next=first->other=0;
  1406.                     first->tree=tree;
  1407.                     killsp();
  1408.                 }else{
  1409.                     free_expression(tree);error(46);
  1410.                     return(0);
  1411.                 }
  1412.             }else{
  1413.                 first=mymalloc(CLS);
  1414.                 first->next=first->other=0;
  1415.                 first->tree=0;
  1416.                 eval_constn(tree);
  1417.                 tree->ntyp->flags=t->flags;
  1418.                 insert_const(tree);
  1419.                 first->val=tree->val;
  1420.                 free_expression(tree2);
  1421.                 killsp();
  1422.             }
  1423.         }else{
  1424.         /*  auch anderes erlaubt    */
  1425.             first=mymalloc(CLS);
  1426.             first->next=first->other=0;
  1427.             first->tree=tree;
  1428.             killsp();
  1429.         }
  1430.     }
  1431.     if(bracket){
  1432.         if(*s==','){s++;killsp();}
  1433.         if(*s=='}'){s++;killsp();} else error(128);
  1434.     }
  1435.     return(first);
  1436. }
  1437.  
  1438.  
  1439.  
  1440.